<!-- src/components/PatientTable.vue -->
<template>
  <div class="container">
    <!-- 患者信息表格区域 -->
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @sort-change="handleSortChange"
      v-if="tableData.length > 0"
    >
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="name" label="姓名" width="100" />
      <el-table-column prop="gender" label="性别" width="80" />
      <el-table-column prop="idCard" label="身份证号" width="180" />
      <el-table-column prop="birthDate" label="出生日期" width="120" sortable />
      <el-table-column prop="phone" label="手机号" width="130" />
      <el-table-column prop="address" label="地址" width="180" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button size="small" type="primary" @click="handleSelect(scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 无数据提示 -->
    <div v-else class="no-data">暂无数据</div>
    <!-- 分页控件 -->
    <el-pagination
      class="mt-4"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[5, 10, 20]"
      :page-size="pageSize"
      v-model:current-page="currentPage"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'

// 接收父组件传入的患者数据
const props = defineProps({
  tableData: {
    type: Array,
    default: () => []
  }
})

// 分页相关
const currentPage = ref(1)
const pageSize = ref(5)
const total = computed(() => props.tableData.length)

// 计算当前页数据
const tableData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return props.tableData.slice(start, end)
})

// 处理分页大小变化
const handleSizeChange = (newSize) => {
  pageSize.value = newSize
}

// 处理页码变化
const handleCurrentChange = (newPage) => {
  currentPage.value = newPage
}

// 选择患者（发出事件）
const emit = defineEmits(['select'])
const handleSelect = (row) => {
  emit('select', row)
  ElMessage.info(`已选择患者: ${row.name}`)
}
</script>

<style scoped>
.container {
  padding: 20px;
  background: #fff;
  border-radius: 8px;
}
.mt-4 {
  margin-top: 1rem;
}
.no-data {
  text-align: center;
  color: #999;
  padding: 32px 0;
}
</style>